<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');
require_once(D_P."data/bbscache/forum_cache.php");
require_once(R_P.'require/showimg.php');

InitGP(array('see'),null,1);
$page = (int)GetGP('page');
$perpage = 20;
$u = (int)GetGP('u');
!$u && $u = $winduid;
$isU = false;
$u == $winduid && $isU = true;
include_once(D_P.'data/bbscache/forum_cache.php');
$fids = getSpecialFid();
$myshortcut = true;
$shortcutforum = array();
if ($winddb['shortcut']) {
	isset($forum) || require(D_P.'data/bbscache/forum_cache.php');
	foreach (explode(',',$winddb['shortcut']) as $key => $value) {
		if ($value && isset($forum[$value])) {
			$shortcutforum[$value] = $forum[$value]['name'];
		}
	}
}
if (empty($shortcutforum)) {
	if (!$db_shortcutforum && $winduid) {
		require_once(R_P.'require/updateforum.php');
		$db_shortcutforum = updateshortcut();
	}
	$shortcutforum = $db_shortcutforum;
	$myshortcut = false;
}


$force = $where = '';
$article = array();
$ordertype = 'postdate';
if ($see == 'all') {
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');
	$timglimit = $timestamp - 86400*3;
	$force = 'FORCE INDEX(postdate)';
	$where .= ($fids ? "fid NOT IN($fids) AND " : '')."ifcheck='1' AND postdate>".pwEscape($timglimit);
	$thisbase = $basename.'q=article&see=all&';
	$username = $windid;
} elseif ($see == 'friend') {
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');
	$thisbase = $basename.'q=article&see=friend&';
	if ($friends = getFriends($winduid)) {
		$uids = array_keys($friends);
		$timglimit = $timestamp - 86400*5;
		!empty($fids) && $where .= "fid NOT IN($fids) AND ";
		$where .= "authorid IN(".pwImplode($uids).") AND ifcheck=1 AND ifhide=0 AND anonymous=0";
		$where .= " AND postdate>".pwEscape($timglimit);
	} else {
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_article');
		footer();
	}
	$username = $windid;
} elseif ($see == 'favor') {
	InitGP(array('job','type'));
	if (empty($job)) {
		!$type && $type = 'all';
		if ($u!=$winduid) {
			if ($friend = getOneFriend($u)) {
				$faceimg = $friend['face'];
				$username = $friend['username'];
				$where = "AND fid NOT IN($fids)";
			} else {
				Showmsg('mode_o_not_friend');
			}
		} else {
			list($faceimg) = showfacedesign($winddb['icon'],1,'s');
			$username = $windid;
			$where = '';
		}
		$favor  = $db->get_one("SELECT tids,type FROM pw_favors WHERE uid=".pwEscape($u));
		Add_S($favor);
		if ($favor['type']) {
			$typeid = explode(',',$favor['type']);
		}
		$tids = $ptids = array();
		if ($tiddb = getfavor($favor['tids'])) {
			if ($type == 'all') {
				foreach ($tiddb as $key => $val) {
					if ($val) {
						$tids += $val;
					}
				}
			} elseif ($tiddb[$type]) {
				$tids = $tiddb[$type];
			}
			$page<1 && $page = 1;
			$start_limit = intval(($page-1) * $perpage);
			$count = count($tids);
			$numofpage = ceil($count/$perpage);
			$numofpage < 1 && $numofpage = 1;
			$page > $numofpage && $page = $numofpage;
			$pages = numofpage($count,$page,$numofpage,$basename."q=article&see=$see&type=$type&u=$u&");
		}
		if ($tids) {
			$tids = pwImplode($tids);
			$query = $db->query("SELECT fid,tid,subject,postdate,author,authorid,anonymous,replies,hits,titlefont FROM pw_threads WHERE tid IN($tids) $where ORDER BY postdate DESC");
			while ($rt = $db->fetch_array($query)) {
				$rt['subject']	= substrs($rt['subject'],45);
				$rt['postdate']	= get_date($rt['postdate']);
				$keyvalue		= get_key($rt['tid'],$tiddb);
				if ($rt['anonymous'] && !in_array($groupid,array('3','4')) && $rt['authorid'] != $winduid) {
					$rt['author']	= $db_anonymousname;
					$rt['authorid'] = 0;
				}
				$type == 'all' && $tidarray[$keyvalue][$rt['tid']] = $rt['tid'];
				$rt['forum']	= $forum[$rt['fid']]['name'];
				$rt['sel']		= $typeid[$keyvalue-1];
				$article[]		= $rt;
			}
			$article = array_slice($article,$start_limit,$perpage);
		}
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_article');
		footer();
	} elseif ($job == 'addtype') {
		PostCheck();
		!$isU && Showmsg('space_over_right');
		(!$type || strlen($type)>20) && Showmsg('favor_cate_error');
		strpos($type,',') !== false && Showmsg('favor_cate_limit');
		$favor   = $db->get_one("SELECT type FROM pw_favors WHERE uid=".pwEscape($winduid));
		$newtype = $favor['type'];
		$newtype.= $newtype ? ",".stripslashes($type) : stripslashes($type);
		$newtype = addslashes(Char_cv($newtype));
		if ($favor) {
			$db->update("UPDATE pw_favors SET type=".pwEscape($newtype)."WHERE uid=".pwEscape($winduid));
		} else{
			$db->update("INSERT INTO pw_favors SET".pwSqlSingle(array('uid'=>$winduid,'type'=>$newtype)));
		}
		refreshto("$basename".'q=article&see=favor','operate_success');
	} elseif ($job == 'clear') {

		PostCheck();
		!$isU && Showmsg('space_over_right');
		InitGP(array('selid'),'P');
		!$selid && Showmsg('sel_error');
		$rs = $db->get_one("SELECT tids FROM pw_favors WHERE uid=".pwEscape($winduid));
		if ($rs) {
			$tiddb = getfavor($rs['tids']);
			foreach ($selid as $key => $tid) {
				foreach ($tiddb as $k => $v) {
					if (in_array($tid,$v)) {
						unset($tiddb[$k][$tid]);
					}
				}
			}
			foreach ($tiddb as $key => $val) {
				if (empty($val)) {
					unset($tiddb[$key]);
				}
			}
			$newtids = makefavor($tiddb);
			$db->update("UPDATE pw_favors SET tids=".pwEscape($newtids)."WHERE uid=".pwEscape($winduid));
			refreshto("$basename".'q=article&see=favor','operate_success');
		} else {
			Showmsg('job_favor_del');
		}
	} elseif ($job == 'change') {

		PostCheck();
		!$isU && Showmsg('space_over_right');
		InitGP(array('selid'),'P');
		!$selid && Showmsg('sel_error');
		$rs = $db->get_one("SELECT tids FROM pw_favors WHERE uid=".pwEscape($winduid));
		if ($rs) {
			$tiddb = getfavor($rs['tids']);
			foreach ($selid as $key => $tid) {
				if (!is_numeric($tid)) continue;
				foreach ($tiddb as $k => $v) {
					if (in_array($tid,$v)) {
						unset($tiddb[$k][$tid]);
					}
				}
				$tiddb[$type][$tid] = $tid;
			}
			foreach ($tiddb as $key => $val) {
				if (empty($val)) {
					unset($tiddb[$key]);
				}
			}
			$newtids = makefavor($tiddb);
			$db->update("UPDATE pw_favors SET tids=".pwEscape($newtids)."WHERE uid=".pwEscape($winduid));
		}
		refreshto("$basename".'q=article&see=favor','operate_success');

	}  elseif ($job == 'deltype') {

		PostCheck();
		!$isU && Showmsg('space_over_right');
		(int)$type<1 && Showmsg('type_error');
		$tnum  = $type-1;
		$rs    = $db->get_one("SELECT tids,type FROM pw_favors WHERE uid=".pwEscape($winduid));
		$tiddb = getfavor($rs['tids']);
		$typedb= explode(',',$rs['type']);
		Add_S($typedb);
		unset($typedb[$tnum]);
		if ($tiddb[$type]) {
			foreach ($tiddb[$type] as $key => $val) {
				$tiddb['0'][$val] = $val;
			}
		}
		unset($tiddb[$type]);
		$newtids = makefavor($tiddb);
		$newtype = Char_cv(implode(',',$typedb));
		$db->update("UPDATE pw_favors SET ".pwSqlSingle(array('tids'=>$newtids,'type'=>$newtype))."WHERE uid=".pwEscape($winduid));
		refreshto("$basename".'q=article&see=favor','operate_success');
	}
} elseif ($see == 'post') {
	InitGP(array('ptable'));
	if ($u!=$winduid) {
		$friend = $db->get_one("SELECT uid,username,icon FROM pw_members WHERE uid=".pwEscape($u));
		list($faceimg) = showfacedesign($friend['icon'],1,'s');
		$username = $friend['username'];
	} else {
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');
		$username = $windid;
	}
	!$page && $page = 1;
	$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
	!isset($ptable) && $ptable = $db_ptable;
	$pw_posts = GetPtable($ptable);

	$fidoff = $isU ? array(0) : getFidoff($groupid);
	$sqloff = ' AND p.fid NOT IN('.pwImplode($fidoff).')';

	$count = $db->get_value("SELECT COUNT(*) AS count FROM $pw_posts p WHERE authorid=".pwEscape($u)." $sqloff");
	$nurl = $basename.'q=article&see=post&u='.$u.'&';
	$p_list = $db_plist ? explode(',',$db_plist) : array();
	if ($p_list) {
		$p_table = "<tr><td id=\"up_post0\" class=\"fav\"><a href=\"{$nurl}ptable=0\">post</a></td></tr>";
		foreach ($p_list as $key => $val) {
			$p_table .= "<tr><td id=\"up_post$val\" class=\"fav\"><a href=\"{$nurl}ptable=$val\">post$val</a></td></tr>";
		}
		$nurl .= "ptable=$ptable&";
	}
	$pages = numofpage($count,$page,ceil($count/$db_perpage),$nurl);
	$isGM = CkInArray($windid,$manager);
	$query = $db->query("SELECT p.pid,p.postdate,t.tid,t.fid,t.subject,t.authorid,t.author,t.replies,t.hits,t.titlefont,t.anonymous FROM $pw_posts p LEFT JOIN pw_threads t USING(tid) WHERE p.authorid=".pwEscape($u)." $sqloff ORDER BY p.postdate DESC $limit");
	while ($rt = $db->fetch_array($query)){
		$rt['subject']	= substrs($rt['subject'],45);
		if ($rt['anonymous'] && $rt['authorid'] != $winduid && !$isGM) {
			$rt['author'] = $db_anonymousname;
			$rt['authorid'] = 0;
		}
		$rt['forum']	= strip_tags($forum[$rt['fid']]['name']);
		$rt['postdate']	= get_date($rt['postdate'],'Y-m-d');
		$article[]		= $rt;
	}
	require_once(R_P.'require/header.php');
	require_once PrintEot('m_article');
	footer();
} elseif ($see == 'trade') {
	InitGP(array('job'));
	!$isU && $job = 'onsale';
	if ($u!=$winduid) {
		$friend = $db->get_one("SELECT uid,username,icon FROM pw_members WHERE uid=".pwEscape($u));
		list($faceimg) = showfacedesign($friend['icon'],1,'s');
		$username = $friend['username'];
	} else {
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');
		$username = $windid;
	}
	(!is_numeric($page) || $page < 1) && $page = 1;
	$db_perpage = 20;
	if (empty($job)) {
		$total = $db->get_value("SELECT COUNT(*) FROM pw_tradeorder WHERE buyer=".pwEscape($u));
		$numofpage = ceil($total/$db_perpage);
		$pages = numofpage($total,$page,$numofpage,$basename."q=article&see=$see&u=$u&");
		$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
		$trade = array();
		$query = $db->query("SELECT td.*,t.icon,m.username FROM pw_tradeorder td LEFT JOIN pw_trade t ON td.tid=t.tid LEFT JOIN pw_members m ON td.seller=m.uid WHERE td.buyer=".pwEscape($u).' ORDER BY td.oid DESC '.$limit);
		while ($rt = $db->fetch_array($query)) {
			$rt['icon'] = goodsicon($rt['icon']);
			$trade[] = $rt;
		}
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_article');
		footer();
	
	} elseif ($job == 'onsale') {
		$total = $db->get_value("SELECT COUNT(*) FROM pw_trade WHERE uid=".pwEscape($u));
		$numofpage = ceil($total/$db_perpage);
		$pages = numofpage($total,$page,$numofpage,$basename."q=article&see=$see&job=$job&u=$u&");
		$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
	
		$trade = array();
		$query = $db->query("SELECT * FROM pw_trade WHERE uid=".pwEscape($u).' ORDER BY tid DESC '.$limit);
		while ($rt = $db->fetch_array($query)) {
			$rt['icon'] = goodsicon($rt['icon']);
			$trade[] = $rt;
		}
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_article');
		footer();
	
	} elseif ($job == 'saled') {
		$total = $db->get_value("SELECT COUNT(*) FROM pw_tradeorder WHERE seller=".pwEscape($u));
		$numofpage = ceil($total/$db_perpage);
		$pages = numofpage($total,$page,$numofpage,$basename."q=article&see=$see&job=$job&u=$u&");
		$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
		$trade = array();
		$query = $db->query("SELECT td.*,t.icon,m.username FROM pw_tradeorder td LEFT JOIN pw_trade t ON td.tid=t.tid LEFT JOIN pw_members m ON td.buyer=m.uid WHERE td.seller=".pwEscape($u).' ORDER BY td.oid DESC '.$limit);
		while ($rt = $db->fetch_array($query)) {
			$rt['icon'] = goodsicon($rt['icon']);
			$trade[] = $rt;
		}
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_article');
		footer();
	}
}else {
	InitGP(array('ordertype'));
	!in_array($ordertype,array('lastpost','postdate')) && $ordertype = 'postdate';
	if ($u!=$winduid) {
		$where .= 'authorid='.pwEscape($u).' AND anonymous=0 AND ifhide=0 AND fid<>0 ';
		$friend = $db->get_one("SELECT uid,username,icon FROM pw_members WHERE uid=".pwEscape($u));
		list($faceimg) = showfacedesign($friend['icon'],1,'s');
		$username = $friend['username'];
		$thisbase = $basename.'q=article&u='.$u.'&ordertype='.$ordertype.'&';
	} else {
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');
		$where .= 'authorid='.pwEscape($winduid);
		$thisbase = $basename.'q=article&ordertype='.$ordertype.'&';
		$username = $windid;
		$u = $winduid;
	}

}
if ($where) {
	$count = $db->get_value("SELECT count(*) as count FROM pw_threads WHERE $where");
	if ($count) {
		!$page && $page = 1;
		$start = ($page-1)*$perpage;
		$numofpage = ceil($count/$perpage);
		$isGM = CkInArray($windid,$manager);
		$rs = $db->query("SELECT tid,fid,author,authorid,subject,postdate,lastpost,replies,hits,titlefont,anonymous FROM pw_threads $force WHERE $where ORDER BY $ordertype DESC ".pwLimit($start,$perpage));
		while ($rt = $db->fetch_array($rs)) {
			$rt['subject'] = substrs($rt['subject'],45);
			$rt['forum'] = strip_tags($forum[$rt['fid']]['name']);
			$rt['postdate'] = get_date($rt['postdate'],'Y-m-d');
			if (!$isGM && $rt['anonymous']) {
				$rt['author'] = $rt['authorid'] = '';
			}
			$article[] = $rt;
		}
		$pages = numofpage($count,$page,$numofpage,$thisbase);
	}
}
require_once(R_P.'require/header.php');
require_once PrintEot('m_article');footer();

function getfavor($tids) {
	$tids  = explode('|',$tids);
	$tiddb = array();
	foreach ($tids as $key => $t) {
		if ($t) {
			$v = explode(',',$t);
			foreach ($v as $k => $v1) {
				$tiddb[$key][$v1] = $v1;
			}
		}
	}
	return $tiddb;
}
function get_key($tid,$tiddb) {
	foreach ($tiddb as $key => $value) {
		if (in_array($tid,$value)) {
			return $key;
		}
	}
}
function getFidoff($gid) {
	global $db;
	$fidoff = array(0);
	$query = $db->query("SELECT fid FROM pw_forums WHERE type<>'category' AND (password!='' OR forumsell!='' OR allowvisit!='' AND allowvisit NOT LIKE '%,$gid,%')");
	while ($rt = $db->fetch_array($query)) {
		$fidoff[] = $rt['fid'];
	}
	return $fidoff;
}

function makefavor($tiddb) {
	$newtids = $ex = '';
	$k = 0;
	ksort($tiddb);
	foreach ($tiddb as $key => $val) {
		$new_tids = '';
		rsort($val);
		if ($key != $k) {
			$s = $key - $k;
			for ($i = 0; $i < $s; $i++) {
				$newtids .= '|';
			}
		}
		foreach ($val as $k => $v) {
			is_numeric($v) && $new_tids .= $new_tids ? ','.$v : $v;
		}
		$newtids .= $ex.$new_tids;
		$k  = $key + 1;
		$ex = '|';
	}
	return $newtids;
}

function goodsicon($icon) {
	global $attachpath,$imgpath;
	if (empty($icon)) {
		return $imgpath.'/goods_none.gif';
	}
	if (file_exists($attachpath.'/thumb/'.$icon)) {
		return $attachpath.'/thumb/'.$icon;
	}
	if (file_exists($attachpath.'/'.$icon)) {
		return $attachpath.'/'.$icon;
	}
	return $imgpath.'/goods_none.gif';
}
?>